package com.kgc.service.listener;

import com.kgc.dao.TbItemMapper;
import com.kgc.entity.SearchItem;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ActiveMqMessageListener implements MessageListener {
    @Autowired
    private SolrServer solrServer;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage=(TextMessage)message;
        Long itemId=null;
        SearchItem searchItem=null;
        try {
            //得到新增加商器编号
            Thread.sleep(1000);
            itemId=Long.parseLong(textMessage.getText());
            System.out.println("itemId:"+itemId);
            searchItem=tbItemMapper.searchItemById(itemId);
            addSearchItemToSolr(searchItem);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void addSearchItemToSolr(SearchItem item) throws Exception{
        SolrInputDocument solrDocument = new SolrInputDocument();
        solrDocument.addField("id",item.getId());
        solrDocument.addField("item_title",item.getTitle());
        solrDocument.addField("item_sell_point", item.getSellPoint());
        solrDocument.addField("item_price", item.getPrice());
        solrDocument.addField("item_category_name", item.getCategoryName());
        solrDocument.addField("item_image",item.getImage());
        solrServer.add(solrDocument);
        solrServer.commit();
    }
}
